unit INamedDeviceClient;

interface
uses
  SysUtils,
  INamedDevice,
  IPollThread,
  IIONamedDeviceThread,
  idata;

type
  EOINamedDeviceClient = class(EOINamedDevice)
  private

  protected
    FDisconnected: Boolean;

    FIONamedDeviceThread: EOIONamedDeviceThread;

    procedure doClose; override;

  public
    property Disconnected: Boolean read FDisconnected;

    //function Read(Data: EOIOBuffer): Boolean; virtual;
    function Write(Data: EOIOBuffer): Boolean; virtual;

    procedure RemoveThread(ThreadInstance: EOIPollThread); override;

  end;

implementation

procedure EOINamedDeviceClient.doClose;
begin
  if not FDisconnected then begin
    FDisconnected := True;
    FIONamedDeviceThread.Terminate;
    FIONamedDeviceThread := Nil;
  end;
end;

{
function EOINamedDeviceClient.Read(Data: EOIOBuffer): Boolean;
var
  tmpI: Integer;
begin
  Result := True;

  if Active and FDisconnected then
    Active := False;

  if not Active then
    Active := True;

  if Active then
    if FIONamedDeviceThread = Nil then
      doOpen;

  if Active then begin
    if FIONamedDeviceThread <> Nil then begin
      tmpI := InterlockedIncrement(FDataInLock);
      try
        if tmpI = 1 then begin
          Data.Flush;
          Data.Push(FDataIn);
          FDataIn.Flush;
        end;
      finally
        InterlockedDecrement(FDataInLock);
      end;
    end;
  end;
end;
}

function EOINamedDeviceClient.Write(Data: EOIOBuffer): Boolean;
begin
  Result := False;

  if Active and FDisconnected then
    Active := False;

  if not Active then
    Active := True;

  if Active then
    if FIONamedDeviceThread = Nil then
      doOpen;

  if Active then begin
    if FIONamedDeviceThread <> Nil then begin
      if FIONamedDeviceThread.LockOutData(1000) then begin
        try
          FIONamedDeviceThread.OutData.Push(Data);
          Result := True;
        finally
          FIONamedDeviceThread.UnlockOutData;
        end;
        Sleep(1);
      end;
    end;
  end;

end;

procedure EOINamedDeviceClient.RemoveThread(ThreadInstance: EOIPollThread);
begin
  FDisconnected := True;
end;

end.

